{SysSrv} = require "./SysSrv"

SupperClz = SysSrv
exports.EmpT2Srv = new Class
  Extends: SupperClz
  options:
    tab: "emp"
    sld_eny: {}
  onDraw: ->
    t = this
    o = t.options
    yield SupperClz.prototype.onDraw.apply t,arguments
    return
  hInclude: (reqOpt)->
    t = this
    o = t.options
    #获得子页面的Srv实例
    deptListSrv = yield t._srvClz "EmpT2.sys.DeptListSrv||sys.DeptListSrv"
    roleListSrv = yield t._srvClz "EmpT2.sys.RoleListSrv||sys.RoleListSrv"
    deptListSrv.dgJoin = (sql)->
      sql += """
      
      left join dept_emp de
        on de.dept_id=t.id
      """
      sql
    deptSrch2Whr = deptListSrv.srch2Whr
    deptListSrv.srch2Whr = (reqOpt,argArr,seaArr,isWhr)->
      whr = deptSrch2Whr.apply deptListSrv,arguments
      if o.sld_eny.id
        index = argArr.push o.sld_eny.id
        if whr.indexOf(" where ") is -1
          whr += " where de.emp_id=$"+index
        else
          whr += " and de.emp_id=$"+index
      whr
    roleSrch2Whr = roleListSrv.srch2Whr
    roleListSrv.srch2Whr = (reqOpt,argArr,seaArr,isWhr)->
      whr = roleSrch2Whr.apply roleListSrv,arguments
      if o.sld_eny.id
        index = argArr.push o.sld_eny.id
        if whr.indexOf(" where ") is -1
          whr += " where de.emp_id=$"+index
        else
          whr += " and de.emp_id=$"+index
      whr
    #删除
    deptDelById = deptListSrv.delById
    deptListSrv.delById = (reqOpt,id,tab)->
      rltSet = yield deptDelById.apply this,arguments
      sql = "delete from dept_emp where dept_id=$1"
      argArr = [id]
      yield t.callSql reqOpt,sql,argArr
      rltSet
    roleDelById = roleListSrv.delById
    roleListSrv.delById = (reqOpt,id,tab)->
      rltSet = yield roleDelById.apply this,arguments
      sql = "delete from role_emp where role_id=$1"
      argArr = [id]
      yield t.callSql reqOpt,sql,argArr
      rltSet
    #增加
    deptAddSrv = yield t._srvClz "EmpT2.sys.DeptAddSrv||sys.DeptAddSrv"
    deptSaveAddClk = deptAddSrv.saveAddClk
    deptAddSrv.saveAddClk = (reqOpt,entry,keyArr,returning,tab)->
      rltSet = yield deptSaveAddClk.apply this,arguments
      sql = "insert into dept_emp(dept_id,emp_id) values($1,$2)"
      argArr = [rltSet[0].id,o.sld_eny.id]
      yield t.callSql reqOpt,sql,argArr
      rltSet
    roleAddSrv = yield t._srvClz "EmpT2.sys.RoleAddSrv||sys.RoleAddSrv"
    roleSaveAddClk = roleAddSrv.saveAddClk
    roleAddSrv.saveAddClk = (reqOpt,entry,keyArr,returning,tab)->
      rltSet = yield roleSaveAddClk.apply this,arguments
      sql = "insert into role_emp(role_id,emp_id) values($1,$2)"
      argArr = [rltSet[0].id,o.sld_eny.id]
      yield t.callSql reqOpt,sql,argArr
      rltSet
    return
  "@prn_trSld":{comment:"父页面选中一行时,给子页面增加筛选条件"}
  prn_trSld: (reqOpt,sld_eny)->
    t = this
    o = t.options
    o.sld_eny = sld_eny
    return
  optRmvDeptList: (reqOpt,id)->
    t = this
    o = t.options
    sql = "delete from dept_emp where dept_id=$1"
    argArr = [id]
    yield t.callSql reqOpt,sql,argArr
    return
  slt_butDeptList: (reqOpt,idArr)->
    t = this
    o = t.options
    for id in idArr
      rltSet = yield t.callSql reqOpt,"select t.id from dept_emp t where t.emp_id=$1 and t.dept_id=$2",[o.sld_eny.id,id]
      rltSet = rltSet.rows
      continue if rltSet.length > 0
      yield t.callSql reqOpt,"insert into dept_emp(emp_id,dept_id) values ($1,$2)",[o.sld_eny.id,id]
    return
  optRmvRoleList: (reqOpt,id)->
    t = this
    o = t.options
    sql = "delete from role_emp where role_id=$1"
    argArr = [id]
    yield t.callSql reqOpt,sql,argArr
    return
  slt_butRoleList: (reqOpt,idArr)->
    t = this
    o = t.options
    for id in idArr
      rltSet = yield t.callSql reqOpt,"select t.id from role_emp t where t.emp_id=$1 and t.role_id=$2",[o.sld_eny.id,id]
      rltSet = rltSet.rows
      continue if rltSet.length > 0
      yield t.callSql reqOpt,"insert into role_emp(emp_id,role_id) values ($1,$2)",[o.sld_eny.id,id]
    return
  